package per.zqx.rabbit.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitConfig,
 *
 * @author Sa, Zhang 2021-06-06 14:56
 */
@Configuration
public class RabbitFanoutConfig {
    // 声明交换机
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanout_order_exchange", true, false);
    }

    @Bean
    public FanoutExchange fanoutTtlExchange() {
        return new FanoutExchange("fanout_ttl_order_exchange", true, false);
    }

    // 声明SMS、EMAIL、duanxin队列
    // 测试每条消息设置过期时间
    @Bean
    public Queue ttlFanoutSmsQueue() {
        return new Queue("sms.ttl.fanout.queue", true, false, false);
    }

    // 为队列设置设置
    @Bean
    public Queue ttlFanoutSmsTtlQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl",5000);
        return new Queue("sms.ttl_queue.fanout.queue", true, false, false, args);
    }

    @Bean
    public Queue fanoutSmsQueue() {
        return new Queue("sms.fanout.queue", true, false, false);
    }

    @Bean
    public Queue fanoutEmailQueue() {
        return new Queue("email.fanout.queue", true, false, false);
    }

    @Bean
    public Queue fanoutDuanxinQueue() {
        return new Queue("duanxin.fanout.queue", true, false, false);
    }

    // 队列绑定交换机
    @Bean
    public Binding ttlFanoutSmsBinding() {
        return BindingBuilder.bind(ttlFanoutSmsTtlQueue()).to(fanoutTtlExchange());
    }

    @Bean
    public Binding ttlQueueFanoutSmsBinding() {
        return BindingBuilder.bind(ttlFanoutSmsQueue()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutSmsBinding() {
        return BindingBuilder.bind(fanoutSmsQueue()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutEmailBinding() {
        return BindingBuilder.bind(fanoutEmailQueue()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutDuanxinBinding() {
        return BindingBuilder.bind(fanoutDuanxinQueue()).to(fanoutExchange());
    }

}
